
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}

# Load function for making tex table rows
source(file.path(root, "code/tables_figures/prep_table_rows.R"))

# Bring in the results from the alternative estimators
load(file.path(root, "tmp/robustness_tables.RData"))

# Add the output to a matrix
estimators = c("SDID_NoInt", "SC_Reg", "Ebal", "AIPW_Forest", "AIPW_SL")
alt_out = matrix(nrow=5, ncol=10)
alt_out[1:2,] = cbind(demvs_table[,estimators], tur_table[,estimators])
alt_out[3:5, 1:2] = obs_table$obs_panel_vs
alt_out[3:5, 3:5] = obs_table$obs_wide_vs
alt_out[3:5, 6:7] = obs_table$obs_panel_tur
alt_out[3:5, 8:10] = obs_table$obs_wide_tur

# Put the alternative Dem vote share effect estimates in a tex table
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Advantage Democrats in 2020, Alternative Estimators.}",
"\\label{tab:alt_est_dem}}",
"\\begin{tabular}{lccccc}",
"\\toprule \\toprule",
" & \\multicolumn{5}{c}{Dem Vote Share (\\%)} \\\\",
" & (1) & (2) & (3) & (4) & (5) \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(alt_out[1,1:5]), " \\\\"),
paste0(prep_table_rows(alt_out[2,1:5], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(alt_out[3,1:5], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(alt_out[4,1:5], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(alt_out[5,1:5], wholenum=TRUE), " \\\\"),
"\\midrule",
"Estimator & SDID Without & Synthetic & Entropy & Causal & Super \\\\",
" & Intercept & Control & Balancing & Forest & Learner \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{6}{p{0.9\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data for columns 1, 2, 6, and 7 is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020. Data for columns 3, 4, 5, 8, 9, and 10 is wide with 7 lags of the dependent variable included. ",
"Synthetic control is a regularized synthetic control. SDID without intercept is synthetic difference-in-differences without county fixed effects. ",
"Entropy balancing is maximum entropy reweighting to balance grant recipients and non-recipients on the average of each lag of the outcome. ",
"Causal forest is double machine learning using random forests for both the outcome and propensity models. ",
"Super learner is double machine learning using an ensemble learner for both the outcome and propensity models.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/dem_effect_alt_est_table.tex"), sep="\n")

# Put the alternative turnout effect estimates in a tex table
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Increase Turnout in 2020, Alternative Estimators.}",
"\\label{tab:alt_est_turnout}}",
"\\begin{tabular}{lccccc}",
"\\toprule \\toprule",
" & \\multicolumn{5}{c}{Turnout (\\%)} \\\\",
" & (1) & (2) & (3) & (4) & (5) \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(alt_out[1, 6:10]), " \\\\"),
paste0(prep_table_rows(alt_out[2, 6:10], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(alt_out[3, 6:10], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(alt_out[4, 6:10], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(alt_out[5, 6:10], wholenum=TRUE), " \\\\"),
"\\midrule",
"Estimator & SDID Without & Synthetic & Entropy & Causal & Super \\\\",
" & Intercept & Control & Balancing & Forest & Learner \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{6}{p{0.9\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data for columns 1, 2, 6, and 7 is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020. Data for columns 3, 4, 5, 8, 9, and 10 is wide with 7 lags of the dependent variable included. ",
"Synthetic control is a regularized synthetic control. SDID without intercept is synthetic difference-in-differences without county fixed effects. ",
"Entropy balancing is maximum entropy reweighting to balance grant recipients and non-recipients on the average of each lag of the outcome. ",
"Causal forest is double machine learning using random forests for both the outcome and propensity models. ",
"Super learner is double machine learning using an ensemble learner for both the outcome and propensity models.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/turnout_effect_alt_est_table.tex"), sep="\n")


